var utils = require("../BleMash-js/util.js");
var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var app = getApp()
Page({
  data: {
    sceneapplying:-1,
  },
  onLoad () {
    this.setData({
      phoneheight: app.globalData.phoneheight,
      fix_position: app.globalData.fix_position
    })
    Ajax.AsyncPost("/group/get_scene_list/", {groupid: app.globalData.activegroup.id},(data)=>{
      this.setData({scenes: data.scenes});
    });
  },
  bindfocus: function (e) {
    var index = e.currentTarget.dataset.index;
    var temp_str = "scenes[" + index + "].changename"
    this.setData({[temp_str]: true})
  },
  bindblur: function (e) {
    var index = e.currentTarget.dataset.index;
    var temp_changename = "scenes[" + index + "].changename"
    var temp_name = "scenes[" + index + "].name"
    this.setData({
      [temp_changename]: false,
      [temp_name]: this.data.scenes[index].name
    })
  },
  saveinput: function (e) {
    this.setData({
      saveinput: e.detail.value
    })
  },
  scene_rename: function (e) {
    var index = e.currentTarget.dataset.index;
    Ajax.AsyncPost("/group/rename_scene/", {
      sceneid: this.data.scenes[index].id,
      name: this.data.saveinput
    }, (data) => {
      var temp_str = "scenes[" + index + "].name"
      this.setData({
        [temp_str]: this.data.saveinput
      });
    });
  },
  addscene:function(e){
    Ajax.AsyncPost("/group/add_scene/", {
      name: "新增场景",
      meshid: app.globalData.activemesh.id,
      groupid: app.globalData.activegroup.id,
      red: 0,
      green: 0,
      blue: 0,
      warm: 0,
      cold: 0
    }, (data) => {
      this.data.scenes.push(data.scene);
      this.setData({
        scenes: this.data.scenes
      });
    });
  },
  activatescene:function(e) {
    var index = e.currentTarget.dataset.index;
    app.globalData.activescene = this.data.scenes[index];
  },
  deletescene: function(e){
    var index = e.currentTarget.dataset.index;
    Modal.showModal("确认要删除场景：" + this.data.scenes[index].name + "吗？",()=>{
      Ajax.AsyncPost("/group/delete_scene/", { sceneid: this.data.scenes[index].id}, (data) => {
        this.data.scenes.splice(index, 1)
        this.setData({scenes: this.data.scenes})
      });
    })
  },
  scene_on_off(e){
    var index = e.currentTarget.dataset.index;
    var scene = this.data.scenes[index];
    this.apply(scene);
    this.setData({sceneapplying:index});
  },
  apply(scene) {
    Ajax.AsyncPost("/group/get_scene_lights/", { sceneid:scene.id }, (data) => {
      console.log(data);
      for (let i = 0; i < data.scene_lights.length; i++) {
        this.edit_light(data.scene_lights[i].nodeaddress, data.scene_lights[i].lightid, data.scene_lights[i].cold, 
        data.scene_lights[i].warm, data.scene_lights[i].red, data.scene_lights[i].green, data.scene_lights[i].blue);
      }
    });
  },
  edit_light(nodeAddress,lightid, cold, warm, red,green, blue){
    var blemesh = app.globalData.activemesh;
    blemesh.addressControl(nodeAddress,utils.intToHex(cold),utils.intToHex(warm),utils.intToHex(red),utils.intToHex(green),utils.intToHex(blue),(res) => {
      var data = {
        lightid: lightid,
        cold: cold,
        warm: warm,
        red: red,
        green: green,
        blue: blue,
      };
      Ajax.AsyncPost("/light/edit_light/", data);
    });
  }
})
